#include <iostream>
#include <stack>
using namespace std;


struct node{
	int num,index;
};


int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	stack<node> s1,s2;
	int sum1[n],sum2[n];
	for(int i=0;i<n;i++){
		while(!s1.empty()  &&  s1.top().num <= a[i]) s1.pop();
		if(s1.empty())
			sum1[i] = 0;
		else
			sum1[i] = s1.top().index+1;
		s1.push({a[i],i});
	}
	for(int i=n-1;i>=0;i--){
		while(!s2.empty()  &&  s2.top().num <= a[i]) s2.pop();
		if(s2.empty())
			sum2[i] = n-1;
		else
			sum2[i] = s2.top().index-1;
		s2.push({a[i],i});
	}
	
//	for(int i=0;i<n;i++){
//		cout<<sum1[i]<<" "<<sum2[i]<<endl;
//	}
	
	int ans=0;
	for(int i=0;i<n;i++){
		if(a[i] > ans){
			if(sum2[i]-sum1[i]+1 >= a[i])
				ans = a[i];
		}
	}
	
	cout<<ans;
	return 0;
}
